package site.jlopen.service.imp;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson2.JSON;

import site.jlopen.assets.config.ThreadAuthUser;
import site.jlopen.domain.job.KusciaJob;
import site.jlopen.domain.project.KusciaProject;
import site.jlopen.domain.project.KusciaProjectFlow;
import site.jlopen.domain.project.KusciaProjectGraphParam;
import site.jlopen.domain.project.event.KusciaProjectEvent;
import site.jlopen.entity.BaseResponse;
import site.jlopen.mapper.job.KusciaJobMapper;
import site.jlopen.mapper.job.KusciaJobTaskMapper;
import site.jlopen.mapper.project.KusciaProjectFlowMapper;
import site.jlopen.mapper.project.KusciaProjectGraphParamMapper;
import site.jlopen.mapper.project.KusciaProjectMapper;
import site.jlopen.secret.model.job.SecretJobCreateParam;
import site.jlopen.secret.model.job.SecretJobCreateReq;
import site.jlopen.secret.model.job.SecretJobLogResp;
import site.jlopen.secret.model.job.SecretJobTaskState;
import site.jlopen.service.intf.project.JobTaskService;
import site.jlopen.utils.id.IdUtils;

@Service
public class JobTaskConvertForApiService {
	
	@Autowired
	private KusciaProjectMapper projectMapper;
	@Autowired
	private KusciaProjectFlowMapper flowMapper;
	@Autowired
	private KusciaProjectGraphParamMapper paramMapper;
	@Autowired
	public JobTaskService jobTaskService;
	@Autowired
	private KusciaJobMapper jobMapper;
	@Autowired
	private KusciaJobTaskMapper taskMapper;

	public BaseResponse<String> create(SecretJobCreateReq req) {
		KusciaProject pEvent = projectMapper.selectByInfo(new KusciaProjectEvent().setProjectUserId(ThreadAuthUser.id()).setProjectName("API-" + req.getProjectCode()));
		if(null == pEvent) {
			return new BaseResponse<String>().error("项目编号"+req.getProjectCode()+"不存在");
		}
		Long flowId = flowMapper.selectDefFlow(pEvent.getProjectId());
		if(null == flowId) {
			return new BaseResponse<String>().error("项目训练流不存在");
		}
		if(null == req.getGraphJson()) {
			return new BaseResponse<String>().error("请设置流程JSON");
		}
		List<SecretJobCreateParam> params = req.getParams();
		if(null == params) {
			return new BaseResponse<String>().error("请设置流程参数");
		}
		List <KusciaProjectGraphParam> plist = new ArrayList<KusciaProjectGraphParam>();
		for (SecretJobCreateParam param : params) {
			KusciaProjectGraphParam graphParam = new KusciaProjectGraphParam();
			graphParam.setParamId(IdUtils.SnowFlakeId());
			graphParam.setParamFlowId(flowId);
			graphParam.setParamGraphId(param.getGraphId());
			graphParam.setParamGraphCode(param.getGraphCode());
			graphParam.setParamAttr(JSON.toJSONString(param.getArrts()));
			graphParam.setParamOutputs(JSON.toJSONString(param.getOuputs()));
			graphParam.setParamCreate(new Date());
			plist.add(graphParam);
		}
		KusciaProjectFlow flow = new KusciaProjectFlow();
		flow.setFlowId(flowId);
		flow.setFlowJson(req.getGraphJson());
		flow.setFlowCallback(req.getCallback());
		flowMapper.updateSelective(flow);
		paramMapper.clearParams(flowId);
		paramMapper.insertBatch(plist);
		return new BaseResponse<String>().success("项目任务参数创建成功");
	}

	public BaseResponse<String> start(String projectCode) {
		KusciaProject pEvent = projectMapper.selectByInfo(new KusciaProjectEvent().setProjectUserId(ThreadAuthUser.id()).setProjectName("API-" + projectCode));
		if(null == pEvent) {
			return new BaseResponse<String>().error("项目编号"+projectCode+"不存在");
		}
		Long flowId = flowMapper.selectDefFlow(pEvent.getProjectId());
		return jobTaskService.startJob(pEvent.getProjectId(), flowId, null, 0);
	}

	public BaseResponse<String> restart(String projectCode) {
		KusciaProject pEvent = projectMapper.selectByInfo(new KusciaProjectEvent().setProjectUserId(ThreadAuthUser.id()).setProjectName("API-" + projectCode));
		if(null == pEvent) {
			return new BaseResponse<String>().error("项目编号"+projectCode+"不存在");
		}
		Long flowId = flowMapper.selectDefFlow(pEvent.getProjectId());
		return jobTaskService.startJob(pEvent.getProjectId(), flowId, null, 2);
	}

	public BaseResponse<SecretJobLogResp> logs(String projectCode) {
		KusciaProject pEvent = projectMapper.selectByInfo(new KusciaProjectEvent().setProjectUserId(ThreadAuthUser.id()).setProjectName("API-" + projectCode));
		if(null == pEvent) {
			return new BaseResponse<SecretJobLogResp>().error("项目编号"+projectCode+"不存在");
		}
		return logs(pEvent.getProjectId());
	}

	public BaseResponse<SecretJobLogResp> logs(Long projectId) {
		Long flowId = flowMapper.selectDefFlow(projectId);
		KusciaJob jobInfo = jobMapper.selectByInfo(new KusciaJob().setJobProjectId(projectId).setJobFlowId(flowId));
		if(null == jobInfo) {
			return new BaseResponse<SecretJobLogResp>().error("项目任务不存在");
		}
		SecretJobLogResp jobResp = new SecretJobLogResp();
		jobResp.setJobId(jobInfo.getJobId());
		jobResp.setState(jobInfo.getJobStatus());
		List<SecretJobTaskState> tasks = taskMapper.selectTaskForApi(jobInfo.getJobId());
		jobResp.setTasks(tasks);
		return new BaseResponse<SecretJobLogResp>().success("成功").setData(jobResp);
	}

	
}
